#include "stdafx.h"
#include "SRegedit.h"
#include <atlbase.h>

BOOL rw_reg::GetRegStr(CString strKeyName,
	CString strValueName,
	CString& strValue,
	DWORD dwMaxLength,
	HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	dwMaxLength++;
	TCHAR* pBuf = new TCHAR[dwMaxLength];
	lRet = rk.QueryStringValue(strValueName, pBuf, &dwMaxLength);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	else
	{
		strValue = CString(pBuf);
	}
	rk.Close();
	delete[] pBuf;
	return bRet;
}

BOOL rw_reg::GetRegDWORD(CString strKeyName, CString strValueName, DWORD& dwValue, HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	lRet = rk.QueryDWORDValue(strValueName, dwValue);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	rk.Close();
	return bRet;
}

BOOL rw_reg::GetRegBinary(CString strKeyName,
	CString strValueName,
	BYTE* pBuf,
	DWORD& dwBytes,
	HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	lRet = rk.QueryBinaryValue(strValueName, pBuf, &dwBytes);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	rk.Close();
	return bRet;
}

BOOL rw_reg::SetRegStr(CString strKeyName, CString strValueName, CString strValue, HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	lRet = rk.SetStringValue(strValueName, strValue);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	rk.Close();
	return bRet;
}

BOOL rw_reg::SetRegDWORD(CString strKeyName, CString strValueName, DWORD dwValue, HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	lRet = rk.SetDWORDValue(strValueName, dwValue);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	rk.Close();
	return bRet;
}

BOOL rw_reg::SetRegBinary(CString strKeyName,
	CString strValueName,
	BYTE* pBuf,
	DWORD dwBytes,
	HKEY hKeyParent)
{
	BOOL bRet = TRUE;
	CRegKey rk;
	LONG lRet = rk.Open(hKeyParent, strKeyName);
	if (lRet != ERROR_SUCCESS)
		lRet = rk.Create(hKeyParent, strKeyName);

	lRet = rk.SetBinaryValue(strValueName, pBuf, dwBytes);
	if (lRet != ERROR_SUCCESS)
	{
		bRet = FALSE;
	}
	rk.Close();
	return bRet;
}

BOOL rw_reg::DelRegSubKey(CString strKeyName, CString strSubKey, HKEY hKeyParent, bool bRecurse)
{
	CRegKey rk;
	if (rk.Open(hKeyParent, strKeyName) == ERROR_SUCCESS)
	{
		LONG lRet;
		if (bRecurse)
			lRet = rk.RecurseDeleteKey(strSubKey);
		else
			lRet = rk.DeleteSubKey(strSubKey);
		rk.Close();
		return lRet == ERROR_SUCCESS;
	}
	else
	{
		return FALSE;
	}
}

BOOL rw_reg::DelRegValue(CString strKeyName, CString strValueName, HKEY hKeyParent)
{
	CRegKey rk;
	if (rk.Open(hKeyParent, strKeyName) == ERROR_SUCCESS)
	{
		rk.DeleteValue(strValueName);
		rk.Close();
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

